<?
function stat_(){
        global $session_db_name, $dbh,$baseURL;
	//--------
        $SQL = "select count(*) as cnt from resellers";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[resellers] = $row[cnt];
        //--------	
        $SQL = "select count(distinct id) as cnt from sales";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[active_resellers] = $row[cnt];        	       
 	//--------        
        $SQL = "select count(*) as cnt from domains";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[reseller_domains] = $row[cnt];
        //--------
	$SQL = "select sum(price) as price,count(*) as cnt,sale_data from sales where type='plan' and state!='refund' group by sale_data;";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	while($row = @mysql_fetch_array($result)){
	 	$data[$row[sale_data]]= $row[cnt];
		$data[$row[sale_data].'_']= $row[price];
	}
 	//--------        
        $SQL = "select  sum(price) as price,count(sale_data) as cnt from sales where type='domain' and state!='refund' group by type";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[domains] = $row[cnt];	
	$data[domains_] = $row[price];
	//--------
        $SQL = "select  sum(price) as price,count(price) as cnt from sales where type='upgrade' and state!='refund' group by type";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[upgrades] = $row[cnt];
        $data[upgrades_] = $row[price];
	//--------
        $SQL = "select sum(price) as s1,sum(real_price) as s2 from sales where state!='refund'";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[sump] = $row[s1];	
        $data[sumrp] = $row[s2];	
	//--------	
        $SQL = "select sum(price) as s1 ,count(price) as s2 from sales where state='refund'";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        $data[refunds] = $row[s1];
	$data[refunds_count] = $row[s2];
        //--------
        $SQL = "select count(*) as cnt ,template from interface group by template";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        while($row = @mysql_fetch_array($result)){
                $data[template][$row[template]] = $row[cnt];
        }	
	//--------
        return $data;
}

$data =  stat_();
?>



<CENTER>
<TABLE border=1 class='bTable' cellspacing='0'>
	<TR>
		<TH colspan='3'>Global</TH>
	</TR>
	<TR>
		<TD>Resellers : </TD>
		<TD colspan='2'><?=$data[resellers];?></TD>
	</TR>
	<TR>
		<TD>Active resellers : </TD>
		<TD  colspan='2'><?=$data[active_resellers];?></TD>
	</TR>
	<TR>
		<TD>Reseller's domains count: </TD>
		<TD  colspan='2'><?=$data[reseller_domains];?></TD>
	</TR>	
	<TR>
		<TH colspan='3'>Sales</TH>
	</TR>
	<TR>
		<TD>Business</TD>
		<TD><?=$data[Business];?></TD>
		<TD>$<?=$data[Business_];?></TD>
	</TR>
	<TR>
		<TD>Corporate</TD>
		<TD><?=$data[Corporate];?></TD>
		<TD>$<?=$data[Corporate_];?></TD>
	</TR>
	<TR>
		<TD>Domains</TD>
		<TD><?=$data[domains];?></TD>
		<TD>$<?=$data[domains_];?></TD>
	</TR>
        <TR>
                <TD>Upgrades</TD>
                <TD><?=$data[upgrades];?></TD>
                <TD>$<?=$data[upgrades_];?></TD>
        </TR>
	<TR>
		<TD>Refunds</TD>
		<TD><?=$data[refunds_count];?></TD>
		<TD>$<?=$data[refunds];?></TD>
	</TR>

	<TR>
		<TD>Total : </TD>
		<TD  colspan='2'>$<?=$data[sump];?> / $<?=$data[sumrp];?></TD>
	</TR>
	
	<TR>
		<TH colspan='3'>Templates</TH>
	</TR>
	<TR>
		<TD  colspan='3'>
		<table width='100%'>
<?
$p=0;
while(list($k,$v) = each($data[template])){
	if($p%5==0) $t .= "</tr><tr>";
	$t .= "<td valign='top' align='center'><a target='_blank'href='http://www.resellerspanel.com/images/r$k"."_big.jpg'><img src='http://www.resellerspanel.com/images/r$k.jpg' width='50' height='50' border='0'></a><br>$v</td>";
	$p++;
}
echo $t;
?>
		</table>
		</TD>
	</TR>
</TABLE>
